package com.ruoyi.project.module.lyq.controller;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.project.domain.YhAppointment;
import com.ruoyi.project.tool.HttpUtils;
import com.ruoyi.project.tool.WeChatAccessTokenUtils;
import com.ruoyi.project.tool.WeChatUtils;

import java.text.SimpleDateFormat;
import java.util.Locale;


public class LyqWeChatMassage {

    // 修改这里：方法参数可以更加内容进行修改，userid是必备的
    public static void sendWeChatMassage(String userId, String username, String instrumentName,String storageLocation,String appointmentStart,String appointmentEnd,String specification,String instrumentId) throws JSONException {

        System.out.println(userId);

        //基础企业信息和secret
        String corpid = "ww3c8d31017de5aa90";
        String corpsecret = "RYETl8kUJI8Nec1aWAGh9Q5wnRCC42UzaCuCq4aN5EQ";//设备预约系统，已经修改好
//        //时间标准化
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
//        String timeS = simpleDateFormat.format(yhAppointment.getAppointmentStart());
//        String timeE = simpleDateFormat.format(yhAppointment.getAppointmentEnd());
        // 获取access_token json字符串
        String accessToken = WeChatAccessTokenUtils.getAccessToken(corpid, corpsecret);
        // 发送应用消息
        String userid_url = WeChatUtils.QY_WEIXIN_MESSAGE_PUSH
                .replace("ACCESS_TOKEN", accessToken);

        JSONObject obj1 = new JSONObject();

        // 修改这里：可以根据userid查询用户其他信息，例如用姓名等 待完善

        // 修改这里：消息内容自己拼接

        // 接口文档地址：https://developer.work.weixin.qq.com/document/path/90236

        obj1.put(
//                "content", "【设备预约系统消息】"+ username +" ("+userId+") "+"您好，您有一条待审核的设备预约信息！请前往审核。"
                "content", "【设备预约系统消息】\n" +
                        username + " (" + userId + ") " + "您好，您有一条待审核的设备预约信息！\n\n" +
                        "设备编号：" + instrumentId + "\n" +
                        "预约设备：" + instrumentName + "\n" +
                        "设备规格：" + specification + "\n" +
                        "存放位置：" + storageLocation + "\n" +
                        "预约时间：" + appointmentStart + "至" + appointmentEnd + "\n\n" +
                        "请尽快完成审核。<a href=\"http://spsbyy.bhu.edu.cn/prod-api/system/qywx/getCode\">详细信息查看</a>。"
        );

        JSONObject obj = new JSONObject();

        obj.put("touser", userId);
        obj.put("msgtype", "text");
        obj.put("agentid", "1000185");//设备预约系统，已经修改好
        obj.put("text", obj1);

        // 执行消息推送
        HttpUtils.sendPost(userid_url, obj.toString());
        System.out.println(obj.toString());

    }

//    public static void main(String[] args) {
//        sendWeChatMassage("031052");
//    }
}
